<?php
class FeaturedContent extends BlockInstance {
	public $CI;
	
	function FeaturedContent()
	{
		$this->CI =& get_instance();
		
		$this->CI->load->library('engagement');
		$this->CI->load->model('Block_model');
		$this->CI->load->model('Content_model');
		$this->CI->load->model('Bricabox_model');
		$this->CI->load->model('Membership_model');
		$this->CI->load->model('Activity_model');
		$this->CI->load->helper('inflector');
		$this->CI->load->helper('string');
		$this->CI->load->helper('text');
		$this->CI->load->helper('typography');
	}
	
	function __call($m, $a)
	{
		global $BBX;
		
		$bricabox = $BBX['bricabox'];
		$inst	  = $a[0]['data'];
		
		// Load settings
		$query = $this->CI->db->query("SELECT * FROM meta_settings
			WHERE bricabox_id = ? AND block_instance_id = ?
			LIMIT 1",
			array($bricabox->id, $inst->id));
		if (!$query->num_rows())
		{
			// Set up defaults
			$_settings = array(
				'limit' 		=> 15,
				'sort' 			=> 'date_desc',
				'entry_type' 	=> 'all',
				'tagged' 		=> '',
				'categorized' 	=> -1
			);
		}
		else
		{
			$_settings 				= unserialize($query->first_row()->value);
			$_settings['tagged'] 	= trim(
				
					str_replace('_', ' ',
						str_replace(' ', '',
							preg_replace('/[^\:-_a-z0-9 ]/i', '', str_replace(',', ':', $_settings['tagged']))
						)
					)
					
			);
		}
		if (!isset($_settings['limit']) || !$_settings['limit'])
			$_settings['limit'] = 15;
		$parts = $_settings;
		
		// Hit our RESTful entry list to get the desired entries in the format requested
		$curl_url  = str_replace('://', "://{$bricabox->subdomain}.", APP_ROOT_URL);
		$curl_url2 = '/entries' .
			'/sort/' . $parts['sort'] .
			'/limit/' . $parts['limit'] .
			'/entry_type/' . $parts['entry_type'] .
			(@$parts['tagged'] ? '/tagged/' . @$parts['tagged'] : '') .
			(@$parts['categorized'] > 0 ? '/categorized/' . @$parts['categorized'] : '') .
			'/as/xml';
		//echo $curl_url2;
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $curl_url.$curl_url2);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type' => 'text/xml'));
		
		// HTTP auth if staging
		if (APP_ENV == 'staging')
			curl_setopt($ch, CURLOPT_USERPWD, 'bricabox:suite');
		
		$ret = curl_exec($ch);
		curl_close($ch);
		
		// Parse it out
		$xml 	 = new SimpleXMLElement($ret, LIBXML_NOERROR);
		$entries = array();
		if ($xml->count > 0)
		{
			foreach ($xml->entry as $entry)
				$entries[] = $entry;
		}
		
		// Set up the superbar
		$data = array(
			'content_types' => $this->CI->Bricabox_model->get_content_types($BBX['bricabox']->id),
			'ctgys'			=> $this->CI->Bricabox_model->get_categories($BBX['bricabox']->id)
		);
		
		// Render the view
		$data['parts']		= $parts;
		$data['inst']		= $inst;
		$data['bricabox'] 	= $BBX['bricabox'];
		$data['entries'] 	= $entries;
		$data['rss_url']	= str_replace('/as/xml', '/as/rss', $curl_url2);
		$this->CI->load->view('blocks/featured/homepage', $data);
	}
}
?>